package dao.annotations;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import dao.hbt.HibernateUtil;
import dao.interfaces.IDAO;

public abstract class DAO implements IDAO
{
	// La session es estatica para que se pueda compartir entre todos los DAOs
	
	private static SessionFactory sf;
	private static Session session;
	private static boolean mustCloseSession;
	
	protected static SessionFactory getSession()
	{
		if(sf == null){
			sf = HibernateUtil.getSessionFactory();
		}	
		return sf;
	}
	
	public void openSession()
	{
		session = getSession().openSession();
		mustCloseSession = false;
	}
	
	public void closeSession()
	{
		if (session.isConnected()){
			session.close();
		}
		session = null;
		mustCloseSession = false;
	}
	
	public static Session obtenerSession()
	{
		if (session==null){
			session = getSession().openSession();
			mustCloseSession = true;	
		}
		return session;
	}

	public static boolean isMustCloseSession()
	{
		return mustCloseSession;
	}  
}
